public class jingdian {

}

//链表结构实体类

public class Node {
    Node next = null; //下一节点
    int data;         //节点数据

    public Node(int data) {
        this.data = data;
    }
}
public class Node {
    Node next = null; //下一节点
    int data;         //节点数据

    public Node(int data) {
        this.data = data;
    }
}
public class Linked_List {

    Node head = null;    //链表头结点

    public int get_length() {
        int list_length = 0;
        Node cur_node;
        cur_node = head;
        while (cur_node != null) {
            list_length++;
            cur_node = cur_node.next;
        }
        return list_length;
    }

    /**
     * 2、添加
     * 思路：找到链表的末尾节点，把新的数据节点添加在后面
     */
    public void add_Node(int date) {
        Node newnode = new Node(date);
        if (head == null) {     //判断是否只有一个头结点
            head = newnode;
            return;
        } else {
            Node temp = head;
            while (temp.next != null) {  //不为头节点，找到最后一个节点
                temp = temp.next;
            }
            temp.next = newnode;
        }
    }
/**
 * 3、删除
 * 思路：传入下标，根据下标，找到相应的节点,删除节点的前一节点指向删除节点下一节点
 */
    public boolean delete_Node(int index) {
        if (index < 1 || index > get_length()) {
            System.out.println("你所要删除的元素下标输入有问题");
            return false;
        }
        if (index == 1) {
            head = head.next;
            return true;
        }
        Node pre_node = head;
        Node cur_node = pre_node.next;
        int i = 2;
        while (pre_node != null) {
            if (i == index) {
                pre_node.next = cur_node.next;
                return true;
            } else {
                pre_node = pre_node.next;
                cur_node = cur_node.next;
                i++;
            }
        }
        return true;
    }
